package com.fengjue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fengjue.common.RespBean;
import com.fengjue.common.RespPageBean;
import com.fengjue.mapper.EmployeeMapper;
import com.fengjue.pojo.Employee;
import com.fengjue.pojo.EmployeeEc;
import com.fengjue.mapper.EmployeeEcMapper;
import com.fengjue.service.IEmployeeEcService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fengjue
 * @since 2021-03-10
 */
@Service
public class EmployeeEcServiceImpl extends ServiceImpl<EmployeeEcMapper, EmployeeEc> implements IEmployeeEcService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate... beginDateScope) {
        // 开启分页
        Page<Employee> objects = PageHelper.startPage(currentPage, size);
        objects.setCount(false);
        List<Employee> employeeByPage = employeeMapper.getEmployeeByPage(employee, beginDateScope);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setData(employeeByPage);


      Long l = employeeMapper.selectCountAll();
        respPageBean.setTotal(l);

        return respPageBean;


//                Page<Employee> page = new Page<>(currentPage, size);
//                IPage<Employee> employeeIPage = employeeMapper.getEmployeeByPage(page, employee, beginDateScope);
//                RespPageBean respPageBean = new RespPageBean(employeeIPage.getTotal(), employeeIPage.getRecords());
//                return respPageBean;


    }

    @Override
    public RespBean maxWorkID() {
        // 数据库获取对象
        List<Map<String, Object>> maps = employeeMapper
                .selectMaps(
                        new QueryWrapper<Employee>()
                                .select("max(workID)")
                );
        // 获取最终对象
        Object o = maps.get(0).get("max(workID)");
        System.out.println("o = " + o);
        // 对象转int +1
        int i = Integer.parseInt(o.toString()) + 1;
        // 对象转字符串返回
        String format = String.format("%08d", i);
        return RespBean.success(null, format);
    }

    /**
     * 查询员工
     * @param id
     * @return
     */
    @Override
    public List<Employee> getEmployee(Integer id) {



        return employeeMapper.getEmployee(id);
    }
}
